sgdk
maths.h File Reference

Mathematical methods. More...

Go to the source code of this file.

Classes

struct  Vect2D_u16
struct  Vect2D_s16
struct  Vect2D_u32
struct  Vect2D_s32
struct  Vect2D_f16
struct  Mat2D_f16
struct  Vect3D_f16
struct  Mat3D_f16
struct  Vect4D_f16
struct  Mat4D_f16

Defines

#define min(X, Y)   ((X) < (Y))?(X):(Y)
#define max(X, Y)   ((X) > (Y))?(X):(Y)
#define PI   3.14159265358979323846
#define FIX32_INT_BITS   22
#define FIX32_FRAC_BITS   (32 - FIX32_INT_BITS)
#define FIX32_INT_MASK   (((1 << FIX32_INT_BITS) - 1) << FIX32_FRAC_BITS)
#define FIX32_FRAC_MASK   ((1 << FIX32_FRAC_BITS) - 1)
#define FIX32(value)   ((fix32) ((value) * (1 << FIX32_FRAC_BITS)))
#define intToFix32(value)   ((value) << FIX32_FRAC_BITS)
#define fix32ToInt(value)   ((value) >> FIX32_FRAC_BITS)
#define fix32Round(value)   (fix32Frac(value) > FIX32(0.5))?fix32Int(value + FIX32(1)) + 1:fix32Int(value)
#define fix32ToRoundedInt(value)   (fix32Frac(value) > FIX32(0.5))?fix32ToInt(value) + 1:fix32ToInt(value)
#define fix32Frac(value)   ((value) & FIX32_FRAC_MASK)
#define fix32Int(value)   ((value) & FIX32_INT_MASK)
#define fix32Add(val1, val2)   ((val1) + (val2))
#define fix32Sub(val1, val2)   ((val1) - (val2))
#define fix32Neg(value)   (0 - (value))
#define fix32Mul(val1, val2)   (((val1) * (val2)) >> FIX32_FRAC_BITS)
#define fix32Div(val1, val2)   (((val1) << FIX32_FRAC_BITS) / (val2))
#define FIX16_INT_BITS   10
#define FIX16_FRAC_BITS   (16 - FIX16_INT_BITS)
#define FIX16_INT_MASK   (((1 << FIX16_INT_BITS) - 1) << FIX16_FRAC_BITS)
#define FIX16_FRAC_MASK   ((1 << FIX16_FRAC_BITS) - 1)
#define FIX16(value)   ((fix16) ((value) * (1 << FIX16_FRAC_BITS)))
#define intToFix16(value)   ((value) << FIX16_FRAC_BITS)
#define fix16ToInt(value)   ((value) >> FIX16_FRAC_BITS)
#define fix16Round(value)   (fix16Frac(value) > FIX16(0.5))?fix16Int(value + FIX16(1)) + 1:fix16Int(value)
#define fix16ToRoundedInt(value)   (fix16Frac(value) > FIX16(0.5))?fix16ToInt(value) + 1:fix16ToInt(value)
#define fix16Frac(value)   ((value) & FIX16_FRAC_MASK)
#define fix16Int(value)   ((value) & FIX16_INT_MASK)
#define fix16Add(val1, val2)   ((val1) + (val2))
#define fix16Sub(val1, val2)   ((val1) - (val2))
#define fix16Neg(value)   (0 - (value))
#define fix16Mul(val1, val2)   (((val1) * (val2)) >> FIX16_FRAC_BITS)
#define fix16Div(val1, val2)   (((val1) << FIX16_FRAC_BITS) / (val2))
#define fix32ToFix16(value)   (((value) << FIX16_FRAC_BITS) >> FIX32_FRAC_BITS)
#define fix16ToFix32(value)   (((value) << FIX32_FRAC_BITS) >> FIX16_FRAC_BITS)
#define sinFix32(value)   sintab32[(value) & 1023]
#define cosFix32(value)   sintab32[((value) + 256) & 1023]
#define sinFix16(value)   sintab16[(value) & 1023]
#define cosFix16(value)   sintab16[((value) + 256) & 1023]

Functions

u16 random ()
 Return a random u16 integer.
u32 intToBCD (u32 value)
 Binary to Decimal conversion.
u32 distance_approx (s32 dx, s32 dy)
 Return euclidean distance approximation for specified vector.
The returned distance is not 100% perfect but calculation is fast.
void QSort_u8 (u8 *data, u16 left, u16 right)
 Quick sort algo on u8 data array.
void QSort_s8 (s8 *data, u16 left, u16 right)
 Quick sort algo on s8 data array.
void QSort_u16 (u16 *data, u16 left, u16 right)
 Quick sort algo on u16 data array.
void QSort_s16 (s16 *data, u16 left, u16 right)
 Quick sort algo on s16 data array.
void QSort_u32 (u32 *data, u16 left, u16 right)
 Quick sort algo on u32 data array.
void QSort_s32 (s32 *data, u16 left, u16 right)
 Quick sort algo on s32 data array.

Variables

const fix32 sintab32 [1024]
const fix16 sintab16 [1024]

Detailed Description

Mathematical methods.

Author:
Stephane Dallongeville
Date:
08/2011

This unit provides basic maths methods.
You can find a tutorial about how use maths with SGDK here.


Define Documentation

#define cosFix16 (   value)    sintab16[((value) + 256) & 1023]

Compute cosinus of specified value and return it as fix16.
The input value is an integer defined as [0..1024] range corresponding to radian [0..2PI] range.

#define cosFix32 (   value)    sintab32[((value) + 256) & 1023]

Compute cosinus of specified value and return it as fix32.
The input value is an integer defined as [0..1024] range corresponding to radian [0..2PI] range.

#define FIX16 (   value)    ((fix16) ((value) * (1 << FIX16_FRAC_BITS)))

Convert specified value to fix16

Ex : f16 v = FIX16(-27.12);

#define fix16Add (   val1,
  val2 
)    ((val1) + (val2))

Compute and return the result of the addition of val1 and val2 (fix16).

#define fix16Div (   val1,
  val2 
)    (((val1) << FIX16_FRAC_BITS) / (val2))

Compute and return the result of the division of val1 by val2 (fix16).

#define fix16Frac (   value)    ((value) & FIX16_FRAC_MASK)

Return fractional part of the specified value (fix16).

#define fix16Int (   value)    ((value) & FIX16_INT_MASK)

Return integer part of the specified value (fix16).

#define fix16Mul (   val1,
  val2 
)    (((val1) * (val2)) >> FIX16_FRAC_BITS)

Compute and return the result of the multiplication of val1 and val2 (fix16).

#define fix16Neg (   value)    (0 - (value))

Return negate of specified value (fix16).

#define fix16Round (   value)    (fix16Frac(value) > FIX16(0.5))?fix16Int(value + FIX16(1)) + 1:fix16Int(value)

Round the specified value to nearest integer (fix16).

#define fix16Sub (   val1,
  val2 
)    ((val1) - (val2))

Compute and return the result of the substraction of val2 from val1 (fix16).

#define fix16ToFix32 (   value)    (((value) << FIX32_FRAC_BITS) >> FIX16_FRAC_BITS)

Convert specified fix16 value to fix32.

#define fix16ToInt (   value)    ((value) >> FIX16_FRAC_BITS)

Convert fix16 to integer.

#define fix16ToRoundedInt (   value)    (fix16Frac(value) > FIX16(0.5))?fix16ToInt(value) + 1:fix16ToInt(value)

Round and convert the specified fix16 value to integer.

#define FIX32 (   value)    ((fix32) ((value) * (1 << FIX32_FRAC_BITS)))

Convert specified value to fix32

Ex: f32 v = FIX32(34.567);

#define fix32Add (   val1,
  val2 
)    ((val1) + (val2))

Compute and return the result of the addition of val1 and val2 (fix32).

#define fix32Div (   val1,
  val2 
)    (((val1) << FIX32_FRAC_BITS) / (val2))

Compute and return the result of the division of val1 by val2 (fix32).

#define fix32Frac (   value)    ((value) & FIX32_FRAC_MASK)

Return fractional part of the specified value (fix32).

#define fix32Int (   value)    ((value) & FIX32_INT_MASK)

Return integer part of the specified value (fix32).

#define fix32Mul (   val1,
  val2 
)    (((val1) * (val2)) >> FIX32_FRAC_BITS)

Compute and return the result of the multiplication of val1 and val2 (fix32).

#define fix32Neg (   value)    (0 - (value))

Return negate of specified value (fix32).

#define fix32Round (   value)    (fix32Frac(value) > FIX32(0.5))?fix32Int(value + FIX32(1)) + 1:fix32Int(value)

Round the specified value to nearest integer (fix32).

#define fix32Sub (   val1,
  val2 
)    ((val1) - (val2))

Compute and return the result of the substraction of val2 from val1 (fix32).

#define fix32ToFix16 (   value)    (((value) << FIX16_FRAC_BITS) >> FIX32_FRAC_BITS)

Convert specified fix32 value to fix16.

#define fix32ToInt (   value)    ((value) >> FIX32_FRAC_BITS)

Convert fix32 to integer.

#define fix32ToRoundedInt (   value)    (fix32Frac(value) > FIX32(0.5))?fix32ToInt(value) + 1:fix32ToInt(value)

Round and convert the specified fix32 value to integer.

#define intToFix16 (   value)    ((value) << FIX16_FRAC_BITS)

Convert integer to fix16.

#define intToFix32 (   value)    ((value) << FIX32_FRAC_BITS)

Convert integer to fix32.

#define max (   X,
 
)    ((X) > (Y))?(X):(Y)

Returns te highest value between X an Y.

#define min (   X,
 
)    ((X) < (Y))?(X):(Y)

Returns te lowest value between X an Y.

#define PI   3.14159265358979323846

PI number (3,1415..)

#define sinFix16 (   value)    sintab16[(value) & 1023]

Compute sinus of specified value and return it as fix16.
The input value is an integer defined as [0..1024] range corresponding to radian [0..2PI] range.

#define sinFix32 (   value)    sintab32[(value) & 1023]

Compute sinus of specified value and return it as fix32.
The input value is an integer defined as [0..1024] range corresponding to radian [0..2PI] range.


Function Documentation

u32 distance_approx ( s32  dx,
s32  dy 
)

Return euclidean distance approximation for specified vector.
The returned distance is not 100% perfect but calculation is fast.

Parameters:
dxdelta X.
dydelta Y.
u32 intToBCD ( u32  value)

Binary to Decimal conversion.

Parameters:
valueValue to convert.
void QSort_s16 ( s16 *  data,
u16  left,
u16  right 
)

Quick sort algo on s16 data array.

Parameters:
datas16 data pointer.
leftleft index (should be 0).
rightright index (should be table size - 1).
void QSort_s32 ( s32 *  data,
u16  left,
u16  right 
)

Quick sort algo on s32 data array.

Parameters:
datas32 data pointer.
leftleft index (should be 0).
rightright index (should be table size - 1).
void QSort_s8 ( s8 *  data,
u16  left,
u16  right 
)

Quick sort algo on s8 data array.

Parameters:
datas8 data pointer.
leftleft index (should be 0).
rightright index (should be table size - 1).
void QSort_u16 ( u16 *  data,
u16  left,
u16  right 
)

Quick sort algo on u16 data array.

Parameters:
datau16 data pointer.
leftleft index (should be 0).
rightright index (should be table size - 1).
void QSort_u32 ( u32 *  data,
u16  left,
u16  right 
)

Quick sort algo on u32 data array.

Parameters:
datau32 data pointer.
leftleft index (should be 0).
rightright index (should be table size - 1).
void QSort_u8 ( u8 *  data,
u16  left,
u16  right 
)

Quick sort algo on u8 data array.

Parameters:
datau8 data pointer.
leftleft index (should be 0).
rightright index (should be table size - 1).
 All Classes Files Functions Variables Typedefs Enumerations Defines